<template>
  <div class="teacher-dashboard">
    <h1>教师仪表盘</h1>
    <div class="dashboard-grid">
      <div class="dashboard-card">
        <h2>我的课程</h2>
        <ul>
          <li v-for="course in myCourses" :key="course.id">
            {{ course.name }}
            <div class="course-stats">
              <span>学生数: {{ course.studentCount }}</span>
              <router-link :to="`/courses/${course.id}/manage`">管理课程</router-link>
            </div>
          </li>
        </ul>
      </div>

      <div class="dashboard-card">
        <h2>待批改作业</h2>
        <ul>
          <li v-for="homework in pendingHomeworks" :key="homework.id">
            {{ homework.title }}
            <div class="homework-details">
              <span>课程: {{ homework.courseName }}</span>
              <span>未批改: {{ homework.ungraded }}</span>
              <router-link :to="`/homework/${homework.id}/grade`">批改作业</router-link>
            </div>
          </li>
        </ul>
      </div>

      <div class="dashboard-card">
        <h2>课程统计</h2>
        <div class="course-statistics">
          <div class="stat-item">
            <h3>总课程数</h3>
            <p>{{ courseCount }}</p>
          </div>
          <div class="stat-item">
            <h3>总学生数</h3>
            <p>{{ totalStudents }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useCourseStore } from '../stores/course.store'

const courseStore = useCourseStore()
const myCourses = ref([])
const pendingHomeworks = ref([])
const courseCount = ref(0)
const totalStudents = ref(0)

onMounted(async () => {
  myCourses.value = await courseStore.getTeacherCourses()
  pendingHomeworks.value = await courseStore.getPendingHomeworks()
  courseCount.value = myCourses.value.length
  totalStudents.value = myCourses.value.reduce((total, course) => total + course.studentCount, 0)
})
</script>

<style scoped>
.teacher-dashboard {
  padding: 20px;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.dashboard-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.dashboard-card h2 {
  margin-bottom: 15px;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
}

.dashboard-card ul {
  list-style: none;
  padding: 0;
}

.dashboard-card li {
  display: flex;
  flex-direction: column;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}

.course-stats, .homework-details {
  display: flex;
  justify-content: space-between;
  margin-top: 5px;
  font-size: 0.9em;
  color: #666;
}

.course-statistics {
  display: flex;
  justify-content: space-around;
}

.stat-item {
  text-align: center;
}

.stat-item h3 {
  margin-bottom: 10px;
  color: #666;
}

.stat-item p {
  font-size: 1.5em;
  font-weight: bold;
}
</style>
